﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.BLL;
using YouHP.Utility;

namespace YouHP.Pages.User
{
    public class PmInbox : YouHP.Pages.User.UserBasePage
    {
        protected List<MessageInfo> MsgList = new List<MessageInfo>();
      
        protected int pageIndex = 1;
        protected int pageSize = 15;
        protected int pageCount = 0;
        protected int dataCount = 0;
        protected string PagingString = "";


        protected override void ShowPage()
        {
            base.ShowPage();
            this.AddLocation("收件箱", "/User/PmInbox.aspx");
            this.SetTitle("站内消息");

            //将站内消息设置为已读
            MessageBLL.Instance.UpdatePmState(UserData.Id, 1);

            if (IsPost) HandlePost();
            else HandleGet();

        }

        public void HandlePost()
        {
            string[] Pmitemid = Utils.SplitString(RequestTools.GetFormString("pmitemid"), ",");
            bool succ = true;
            List<int> pmids = new List<int>();

            if (succ)
            {
                if (Pmitemid.Length <= 0) succ = false;
                else if (Pmitemid.Length > pageSize) succ = false;
            }
          
            if (succ)
            {
                foreach (string str in Pmitemid)
                {
                    int tmp = Utils.StrToInt(str, -1);
                    if (tmp <= 0)
                    {
                        succ = false;
                        break;
                    }
                    pmids.Add(tmp);
                }
            }
            

            if (succ)
            {
                MessageBLL.Instance.DeleteMessage(pmids.ToArray());
                this.SetUrl("/User/PmInbox.aspx");
                this.SetMetaRefresh(2, "PmInbox.aspx");
                this.AddMsgLine("删除成功");
            }
            else
            {
                this.AddErrLine("删除失败");
            }


        }
        
        public void HandleGet()
        {
            pageIndex = RequestTools.GetQueryInt("page", 1);
            MsgList = MessageBLL.Instance.GetMessageListByUserId(pageSize, pageIndex, UserData.Id,true, out dataCount);
            pageCount = Utils.GetPageCount(dataCount, pageSize);

            string query = System.Web.HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(
                pageIndex,
                pageCount,
                "/User/PmInbox.aspx" + query, "page");

        }

    }
}
